package com.mediatek.demo.smartconnection;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class SettingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        new Thread() {
            @Override
            public void run() {
                try {
                    // 建立连接到远程服务器的Socket
                    Socket socket = new Socket("192.168.123.158",8988);

                    //获取输出流，向服务器端发送信息
                    OutputStream os=socket.getOutputStream();//字节输出流
                    PrintWriter pw=new PrintWriter(os);//将输出流包装为打印流
                    pw.write("androidConnect");
                    pw.flush();
                    socket.shutdownOutput();//关闭输出流

                    InputStream is=socket.getInputStream();
                    BufferedReader in = new BufferedReader(new InputStreamReader(is));
                    String info=null;
                    while((info=in.readLine())!=null){
                        if(info.equals("wifiCompeleted")){
                            JniLoader loader= (JniLoader) getIntent().getSerializableExtra("loader");
                            int retValue = loader.StopSmartConnection();
                            if (retValue != JniLoader.ERROR_CODE_OK) {
                                System.out.println("one");
                            }
                            Intent intent = new Intent();
                            intent.setClass(SettingActivity.this,FinishActivity.class);
                            startActivity(intent);
                        }
                    }
                    is.close();
                    in.close();
                    socket.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }

//    屏蔽返回按键
    @Override
    public boolean onKeyDown(int keyCode,KeyEvent event){
        if(keyCode==KeyEvent.KEYCODE_BACK)
            return true;//不执行父类点击事件
        return super.onKeyDown(keyCode, event);//继续执行父类其他点击事件
    }

}
